{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import lightgbm as lgb\n",
    "import xgboost as xgb\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn import preprocessing\n",
    "\n",
    "pd.set_option('display.max_colwidth', 200) #表中value值显示长度设为200\n",
    "pd.set_option('display.max_columns', 200)\n",
    "pd.set_option('display.max_rows', 200)\n",
    "\n",
    "import gc\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "base = pd.read_csv('./train/base_info.csv')\n",
    "label = pd.read_csv('./train/entprise_info.csv')\n",
    "base = pd.merge(base, label, on=['id'], how='left') #基本信息 标签\n",
    "#缺失值太多\n",
    "drop = ['enttypeitem', 'opto', 'empnum', 'compform', 'parnum',\n",
    "       'exenum', 'opform', 'ptbusscope', 'venind', 'enttypeminu',\n",
    "       'midpreindcode', 'protype', 'reccap', 'forreccap',\n",
    "       'forregcap', 'congro']\n",
    "\n",
    "for f in drop:\n",
    "    del base[f]\n",
    "del base['dom'], base['opscope'] #单一值太多\n",
    "del base['oploc']\n",
    "#拆分年月特征\n",
    "base['year'] = base['opfrom'].apply(lambda x: int(x.split('-')[0]))\n",
    "base['month'] = base['opfrom'].apply(lambda x: int(x.split('-')[1]))\n",
    "del base['opfrom']\n",
    "data = base.copy()\n",
    "\n",
    "\n",
    "num_feat = []\n",
    "cate_feat = []\n",
    "\n",
    "drop = ['id', 'label'] #不需要的特征\n",
    "cat = ['industryphy'] #类别特征\n",
    "for j in list(data.columns): \n",
    "    if j in drop:\n",
    "        continue\n",
    "    if j in cat:\n",
    "        cate_feat.append(j)\n",
    "    else:\n",
    "        num_feat.append(j)\n",
    "        \n",
    "for i in cate_feat:\n",
    "    data[i] = data[i].astype('category')\n",
    "features = num_feat + cate_feat\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import StratifiedKFold, KFold\n",
    "def get_predict_w(model, data, label='label', feature=[], cate_feature=[], random_state=2018, n_splits=5,\n",
    "                  model_type='lgb'):\n",
    "    if 'sample_weight' not in data.keys():\n",
    "        data['sample_weight'] = 1\n",
    "    model.random_state = random_state\n",
    "    predict_label = 'predict_' + label\n",
    "    kfold = KFold(n_splits=n_splits, shuffle=True, random_state=random_state)\n",
    "    data[predict_label] = 0\n",
    "    test_index = (data[label].isnull()) | (data[label] == -1)    #找到要预测的数据集\n",
    "    train_data = data[~test_index].reset_index(drop=True)     #分割出预测集训练集\n",
    "    test_data = data[test_index]\n",
    "\n",
    "    for train_idx, val_idx in kfold.split(train_data):\n",
    "        model.random_state = model.random_state + 1\n",
    "\n",
    "        train_x = train_data.loc[train_idx][feature]\n",
    "        train_y = train_data.loc[train_idx][label]\n",
    "\n",
    "        test_x = train_data.loc[val_idx][feature]\n",
    "        test_y = train_data.loc[val_idx][label]\n",
    "        if model_type == 'lgb':\n",
    "            try:\n",
    "                model.fit(train_x, train_y, eval_set=[(test_x, test_y)], early_stopping_rounds=400,\n",
    "                          eval_metric='mae',\n",
    "                          callbacks=[lgb.reset_parameter(learning_rate=lambda iter: max(0.005, 0.5 * (0.99 ** iter)))],\n",
    "                          categorical_feature=cate_feature,\n",
    "                          sample_weight=train_data.loc[train_idx]['sample_weight'],\n",
    "                          verbose=100)\n",
    "            except:\n",
    "                model.fit(train_x, train_y, eval_set=[(test_x, test_y)], early_stopping_rounds=200,\n",
    "                          eval_metric='mae',\n",
    "                          callbacks=[lgb.reset_parameter(learning_rate=lambda iter: max(0.005, 0.5 * (0.99 ** iter)))],\n",
    "                          categorical_feature=cate_feature,\n",
    "                          sample_weight=train_data.loc[train_idx]['sample_weight'],\n",
    "                          verbose=100)\n",
    "        elif model_type == 'ctb':\n",
    "            model.fit(train_x, train_y, eval_set=[(test_x, test_y)], early_stopping_rounds=200,\n",
    "                      # eval_metric='mae',\n",
    "                      # callbacks=[lgb.reset_parameter(learning_rate=lambda iter: max(0.005, 0.5 * (0.99 ** iter)))],\n",
    "                      cat_features=cate_feature,\n",
    "                      sample_weight=train_data.loc[train_idx]['sample_weight'],\n",
    "                      verbose=100)\n",
    "        train_data.loc[val_idx, predict_label] = model.predict(test_x)\n",
    "        if len(test_data) != 0:                  #预测集的预测\n",
    "            test_data[predict_label] = test_data[predict_label] + model.predict(test_data[feature])\n",
    "    test_data[predict_label] = test_data[predict_label] / n_splits\n",
    "    print((train_data[label], train_data[predict_label]) * 5, train_data[predict_label].mean(),\n",
    "          test_data[predict_label].mean())\n",
    "\n",
    "    return pd.concat([train_data, test_data], sort=True, ignore_index=True), predict_label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] seed is set=2020, random_state=2021 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0404275\tvalid_0's rmse: 0.139019\n",
      "[200]\tvalid_0's l1: 0.0400459\tvalid_0's rmse: 0.138636\n",
      "[300]\tvalid_0's l1: 0.039952\tvalid_0's rmse: 0.138847\n",
      "[400]\tvalid_0's l1: 0.039964\tvalid_0's rmse: 0.138996\n",
      "Early stopping, best iteration is:\n",
      "[5]\tvalid_0's l1: 0.0399212\tvalid_0's rmse: 0.133244\n",
      "[LightGBM] [Warning] seed is set=2020, random_state=2022 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0471788\tvalid_0's rmse: 0.166972\n",
      "[200]\tvalid_0's l1: 0.0469056\tvalid_0's rmse: 0.167127\n",
      "[300]\tvalid_0's l1: 0.0469596\tvalid_0's rmse: 0.167618\n",
      "[400]\tvalid_0's l1: 0.0469469\tvalid_0's rmse: 0.167813\n",
      "Early stopping, best iteration is:\n",
      "[7]\tvalid_0's l1: 0.0424554\tvalid_0's rmse: 0.150708\n",
      "[LightGBM] [Warning] seed is set=2020, random_state=2023 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0461035\tvalid_0's rmse: 0.1622\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's l1: 0.045906\tvalid_0's rmse: 0.154037\n",
      "[200]\tvalid_0's l1: 0.0459438\tvalid_0's rmse: 0.155309\n",
      "Early stopping, best iteration is:\n",
      "[5]\tvalid_0's l1: 0.0423828\tvalid_0's rmse: 0.144638\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] seed is set=2020, random_state=2024 will be ignored. Current value: seed=2020\n",
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0437073\tvalid_0's rmse: 0.156063\n",
      "[200]\tvalid_0's l1: 0.0443549\tvalid_0's rmse: 0.157973\n",
      "[300]\tvalid_0's l1: 0.0443775\tvalid_0's rmse: 0.158395\n",
      "[400]\tvalid_0's l1: 0.0444308\tvalid_0's rmse: 0.158541\n",
      "Early stopping, best iteration is:\n",
      "[4]\tvalid_0's l1: 0.0440183\tvalid_0's rmse: 0.146759\n",
      "[LightGBM] [Warning] seed is set=2020, random_state=2025 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0393121\tvalid_0's rmse: 0.142938\n",
      "[200]\tvalid_0's l1: 0.039381\tvalid_0's rmse: 0.143228\n",
      "[300]\tvalid_0's l1: 0.0394801\tvalid_0's rmse: 0.143612\n",
      "[400]\tvalid_0's l1: 0.0394212\tvalid_0's rmse: 0.143557\n",
      "Early stopping, best iteration is:\n",
      "[11]\tvalid_0's l1: 0.0336561\tvalid_0's rmse: 0.125697\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] seed is set=2020, random_state=2026 will be ignored. Current value: seed=2020\n",
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0469613\tvalid_0's rmse: 0.161943\n",
      "[200]\tvalid_0's l1: 0.0470871\tvalid_0's rmse: 0.162605\n",
      "[300]\tvalid_0's l1: 0.0469027\tvalid_0's rmse: 0.162435\n",
      "[400]\tvalid_0's l1: 0.0469195\tvalid_0's rmse: 0.162393\n",
      "Early stopping, best iteration is:\n",
      "[10]\tvalid_0's l1: 0.044251\tvalid_0's rmse: 0.15228\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] seed is set=2020, random_state=2027 will be ignored. Current value: seed=2020\n",
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0406431\tvalid_0's rmse: 0.142371\n",
      "[200]\tvalid_0's l1: 0.0408345\tvalid_0's rmse: 0.142281\n",
      "[300]\tvalid_0's l1: 0.0408592\tvalid_0's rmse: 0.14282\n",
      "[400]\tvalid_0's l1: 0.0408925\tvalid_0's rmse: 0.142966\n",
      "Early stopping, best iteration is:\n",
      "[12]\tvalid_0's l1: 0.0358556\tvalid_0's rmse: 0.131961\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] seed is set=2020, random_state=2028 will be ignored. Current value: seed=2020\n",
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0420323\tvalid_0's rmse: 0.152838\n",
      "[200]\tvalid_0's l1: 0.0422375\tvalid_0's rmse: 0.154829\n",
      "[300]\tvalid_0's l1: 0.0424214\tvalid_0's rmse: 0.155296\n",
      "[400]\tvalid_0's l1: 0.0424499\tvalid_0's rmse: 0.155406\n",
      "Early stopping, best iteration is:\n",
      "[3]\tvalid_0's l1: 0.0474387\tvalid_0's rmse: 0.139526\n",
      "[LightGBM] [Warning] seed is set=2020, random_state=2029 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0422991\tvalid_0's rmse: 0.156315\n",
      "[200]\tvalid_0's l1: 0.0426392\tvalid_0's rmse: 0.158298\n",
      "[300]\tvalid_0's l1: 0.0427238\tvalid_0's rmse: 0.158635\n",
      "[400]\tvalid_0's l1: 0.042741\tvalid_0's rmse: 0.158769\n",
      "Early stopping, best iteration is:\n",
      "[3]\tvalid_0's l1: 0.0480184\tvalid_0's rmse: 0.140157\n",
      "[LightGBM] [Warning] seed is set=2020, random_state=2030 will be ignored. Current value: seed=2020\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1551: UserWarning: Using categorical_feature in Dataset.\n",
      "  warnings.warn('Using categorical_feature in Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1555: UserWarning: categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is ['industryphy']\n",
      "  'New categorical_feature is {}'.format(sorted(list(categorical_feature))))\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1286: UserWarning: Overriding the parameters from Reference Dataset.\n",
      "  warnings.warn('Overriding the parameters from Reference Dataset.')\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/lightgbm/basic.py:1098: UserWarning: categorical_column in param dict is overridden.\n",
      "  warnings.warn('{} in param dict is overridden.'.format(cat_alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training until validation scores don't improve for 400 rounds\n",
      "[100]\tvalid_0's l1: 0.0435067\tvalid_0's rmse: 0.153823\n",
      "[200]\tvalid_0's l1: 0.0437346\tvalid_0's rmse: 0.156294\n",
      "[300]\tvalid_0's l1: 0.043657\tvalid_0's rmse: 0.156272\n",
      "[400]\tvalid_0's l1: 0.043672\tvalid_0's rmse: 0.156465\n",
      "Early stopping, best iteration is:\n",
      "[3]\tvalid_0's l1: 0.0493763\tvalid_0's rmse: 0.140757\n",
      "(0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "        ... \n",
      "14860    1.0\n",
      "14861    0.0\n",
      "14862    0.0\n",
      "14863    0.0\n",
      "14864    0.0\n",
      "Name: label, Length: 14865, dtype: float64, 0        0.008763\n",
      "1        0.003527\n",
      "2        0.870769\n",
      "3        0.003503\n",
      "4        0.001399\n",
      "           ...   \n",
      "14860    0.903927\n",
      "14861    0.239507\n",
      "14862   -0.000093\n",
      "14863    0.017304\n",
      "14864   -0.000339\n",
      "Name: predict_label, Length: 14865, dtype: float64, 0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "        ... \n",
      "14860    1.0\n",
      "14861    0.0\n",
      "14862    0.0\n",
      "14863    0.0\n",
      "14864    0.0\n",
      "Name: label, Length: 14865, dtype: float64, 0        0.008763\n",
      "1        0.003527\n",
      "2        0.870769\n",
      "3        0.003503\n",
      "4        0.001399\n",
      "           ...   \n",
      "14860    0.903927\n",
      "14861    0.239507\n",
      "14862   -0.000093\n",
      "14863    0.017304\n",
      "14864   -0.000339\n",
      "Name: predict_label, Length: 14865, dtype: float64, 0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "        ... \n",
      "14860    1.0\n",
      "14861    0.0\n",
      "14862    0.0\n",
      "14863    0.0\n",
      "14864    0.0\n",
      "Name: label, Length: 14865, dtype: float64, 0        0.008763\n",
      "1        0.003527\n",
      "2        0.870769\n",
      "3        0.003503\n",
      "4        0.001399\n",
      "           ...   \n",
      "14860    0.903927\n",
      "14861    0.239507\n",
      "14862   -0.000093\n",
      "14863    0.017304\n",
      "14864   -0.000339\n",
      "Name: predict_label, Length: 14865, dtype: float64, 0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "        ... \n",
      "14860    1.0\n",
      "14861    0.0\n",
      "14862    0.0\n",
      "14863    0.0\n",
      "14864    0.0\n",
      "Name: label, Length: 14865, dtype: float64, 0        0.008763\n",
      "1        0.003527\n",
      "2        0.870769\n",
      "3        0.003503\n",
      "4        0.001399\n",
      "           ...   \n",
      "14860    0.903927\n",
      "14861    0.239507\n",
      "14862   -0.000093\n",
      "14863    0.017304\n",
      "14864   -0.000339\n",
      "Name: predict_label, Length: 14865, dtype: float64, 0        0.0\n",
      "1        0.0\n",
      "2        0.0\n",
      "3        0.0\n",
      "4        0.0\n",
      "        ... \n",
      "14860    1.0\n",
      "14861    0.0\n",
      "14862    0.0\n",
      "14863    0.0\n",
      "14864    0.0\n",
      "Name: label, Length: 14865, dtype: float64, 0        0.008763\n",
      "1        0.003527\n",
      "2        0.870769\n",
      "3        0.003503\n",
      "4        0.001399\n",
      "           ...   \n",
      "14860    0.903927\n",
      "14861    0.239507\n",
      "14862   -0.000093\n",
      "14863    0.017304\n",
      "14864   -0.000339\n",
      "Name: predict_label, Length: 14865, dtype: float64) 0.06667315146120459 0.10812847615899075\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:46: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "/code/zzx/anaconda3/envs/zzx/lib/python3.7/site-packages/ipykernel_launcher.py:47: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    }
   ],
   "source": [
    "#import catboost as cb\n",
    "#cb_model = cb.CatBoostRegressor()\n",
    "\n",
    "lgb_model = lgb.LGBMRegressor(\n",
    "    num_leaves=64, reg_alpha=0., reg_lambda=0.01, metric='rmse',\n",
    "    max_depth=-1, learning_rate=0.05, min_child_samples=10, seed=2020,\n",
    "    n_estimators=2000, subsample=0.7, colsample_bytree=0.7, subsample_freq=1,\n",
    "    device='gpu', gpu_platform_id=1,gpu_device_id= 0)\n",
    "\n",
    "data, predict_label = get_predict_w(lgb_model, data, label='label',\n",
    "                                    feature=features, cate_feature=cate_feat,\n",
    "                                    random_state=2020, n_splits=10,  model_type='lgb')\n",
    "\n",
    "data['score'] = data[predict_label]\n",
    "#data['forecastVolum'] = data['lgb'].apply(lambda x: -x if x < 0 else x)\n",
    "df = data[data.label.isnull()][['id', 'score']]\n",
    "df['score'] = df['score'].apply(lambda x: 0 if x<0 else x) #修正\n",
    "df['score'] = df['score'].apply(lambda x: 1 if x>1 else x)\n",
    "df.to_csv('submit.csv', index=False) #submit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>regcap</th>\n",
       "      <th>enttypegb</th>\n",
       "      <th>label</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>340223</td>\n",
       "      <td>M</td>\n",
       "      <td>7513.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>6</td>\n",
       "      <td>340223010010000000</td>\n",
       "      <td>340200000000115392</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1151</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>340222</td>\n",
       "      <td>O</td>\n",
       "      <td>8090.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2017</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>9053.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>6</td>\n",
       "      <td>340202010010000000</td>\n",
       "      <td>400000000000753910</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>100.0</td>\n",
       "      <td>1151</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2020</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>340221</td>\n",
       "      <td>L</td>\n",
       "      <td>7212.0</td>\n",
       "      <td>4500</td>\n",
       "      <td>6</td>\n",
       "      <td>340221010010000000</td>\n",
       "      <td>400000000000013538</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>4540</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2015</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>8810.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>400000000000283237</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>100.0</td>\n",
       "      <td>1130</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2017</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24860</th>\n",
       "      <td>f1c1045b13d18329a2bd99d2a7e2227688c0d69bf1d1e325</td>\n",
       "      <td>340225</td>\n",
       "      <td>O</td>\n",
       "      <td>8131.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340200000000116780</td>\n",
       "      <td>341400000000011622</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2009</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24861</th>\n",
       "      <td>f000950527a6feb6bde38216d7cbbf32e66d3a3a96d4dbda</td>\n",
       "      <td>340207</td>\n",
       "      <td>J</td>\n",
       "      <td>6790.0</td>\n",
       "      <td>4500</td>\n",
       "      <td>6</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>340200000000115797</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>110.0</td>\n",
       "      <td>4533</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2015</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24862</th>\n",
       "      <td>da8691b210adb3f65b43370d3a362f4aa1d3b16b5ba0c9d7</td>\n",
       "      <td>340207</td>\n",
       "      <td>O</td>\n",
       "      <td>8111.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340207030010000000</td>\n",
       "      <td>340200000000115275</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2012</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24863</th>\n",
       "      <td>516ab81418ed215dcbbf0614a7b929e691f8eed153d7bb31</td>\n",
       "      <td>340225</td>\n",
       "      <td>O</td>\n",
       "      <td>8090.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000116750</td>\n",
       "      <td>341400000000015220</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>260.0</td>\n",
       "      <td>1130</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2012</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24864</th>\n",
       "      <td>9c7fa510616a68303d3427d4bfd4b0cf3e4843f2bf3f637a</td>\n",
       "      <td>340222</td>\n",
       "      <td>N</td>\n",
       "      <td>7830.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340222070010000000</td>\n",
       "      <td>340200000020003395</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2011</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>24865 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     id  oplocdistrict  \\\n",
       "0      47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4         340223   \n",
       "1      9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3         340222   \n",
       "2      59b38c56de3836838082cfcb1a298951abfe15e6940c49ba         340202   \n",
       "3      e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818         340221   \n",
       "4      f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb         340202   \n",
       "...                                                 ...            ...   \n",
       "24860  f1c1045b13d18329a2bd99d2a7e2227688c0d69bf1d1e325         340225   \n",
       "24861  f000950527a6feb6bde38216d7cbbf32e66d3a3a96d4dbda         340207   \n",
       "24862  da8691b210adb3f65b43370d3a362f4aa1d3b16b5ba0c9d7         340207   \n",
       "24863  516ab81418ed215dcbbf0614a7b929e691f8eed153d7bb31         340225   \n",
       "24864  9c7fa510616a68303d3427d4bfd4b0cf3e4843f2bf3f637a         340222   \n",
       "\n",
       "      industryphy  industryco  enttype  state               orgid  \\\n",
       "0               M      7513.0     1100      6  340223010010000000   \n",
       "1               O      8090.0     9600      6  340222060010000000   \n",
       "2               R      9053.0     1100      6  340202010010000000   \n",
       "3               L      7212.0     4500      6  340221010010000000   \n",
       "4               R      8810.0     1100      7  340200000000000000   \n",
       "...           ...         ...      ...    ...                 ...   \n",
       "24860           O      8131.0     9600      6  340200000000116780   \n",
       "24861           J      6790.0     4500      6  340200000000000000   \n",
       "24862           O      8111.0     9600      6  340207030010000000   \n",
       "24863           O      8090.0     1100      7  340200000000116750   \n",
       "24864           N      7830.0     9600      6  340222070010000000   \n",
       "\n",
       "                    jobid  adbusign  townsign  regtype  regcap  enttypegb  \\\n",
       "0      340200000000115392         0         0        1    50.0       1151   \n",
       "1      340200000000112114         0         1        1    10.0       9600   \n",
       "2      400000000000753910         0         0        1   100.0       1151   \n",
       "3      400000000000013538         0         1        1    10.0       4540   \n",
       "4      400000000000283237         0         0        1   100.0       1130   \n",
       "...                   ...       ...       ...      ...     ...        ...   \n",
       "24860  341400000000011622         0         1        1    20.0       9600   \n",
       "24861  340200000000115797         0         1        1   110.0       4533   \n",
       "24862  340200000000115275         0         1        1    10.0       9600   \n",
       "24863  341400000000015220         0         1        1   260.0       1130   \n",
       "24864  340200000020003395         0         1        1    10.0       9600   \n",
       "\n",
       "       label  year  month  \n",
       "0        0.0  2019      7  \n",
       "1        NaN  2017      9  \n",
       "2        0.0  2020      9  \n",
       "3        0.0  2015      9  \n",
       "4        0.0  2017     12  \n",
       "...      ...   ...    ...  \n",
       "24860    NaN  2009      2  \n",
       "24861    NaN  2015     12  \n",
       "24862    NaN  2012      5  \n",
       "24863    NaN  2012     10  \n",
       "24864    NaN  2011      5  \n",
       "\n",
       "[24865 rows x 16 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'lightgbm' has no attribute 'XGBRegressor'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-49-60359a3ef15d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel_selection\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mGridSearchCV\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;31m#from lgboost.sklearn import LGBRegressor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mlgb_model\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlgb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mXGBRegressor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#XGB回归任务\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'label'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'lightgbm' has no attribute 'XGBRegressor'"
     ]
    }
   ],
   "source": [
    "#加入网格搜索以获取最优超参\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "#from xgboost.sklearn import XGBRegressor\n",
    "#xgb_model=xgb.XGBRegressor() #XGB回归任务\n",
    "\n",
    "label='label'\n",
    "predict_label = 'predict_' + label\n",
    "data[predict_label] = 0\n",
    "feature=features\n",
    "test_index = (data[label].isnull()) | (data[label] == -1)    #找到要预测的数据集\n",
    "train_data = data[~test_index].reset_index(drop=True)     #分割出预测集训练集\n",
    "test_data = data[test_index]\n",
    "\n",
    "x_train_full=train_data[feature]#训练集\n",
    "y_train_full=train_data[label]\n",
    "x_test_full=test_data[feature]\n",
    "y_test_full=test_data[label]\n",
    "y_train_full"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n",
      "/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  after removing the cwd from sys.path.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>regcap</th>\n",
       "      <th>enttypegb</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>industryphy</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>340222</td>\n",
       "      <td>8090.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2017</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>340207</td>\n",
       "      <td>9019.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340207040010000000</td>\n",
       "      <td>400000000000325767</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2019</td>\n",
       "      <td>9</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>340222</td>\n",
       "      <td>8111.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>15.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2017</td>\n",
       "      <td>7</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>340203</td>\n",
       "      <td>7590.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>6</td>\n",
       "      <td>340203010010000000</td>\n",
       "      <td>340200000000100093</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1130</td>\n",
       "      <td>2016</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>340202</td>\n",
       "      <td>9051.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>6</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>400000000000386531</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1130</td>\n",
       "      <td>2018</td>\n",
       "      <td>5</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24860</th>\n",
       "      <td>340225</td>\n",
       "      <td>8131.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340200000000116780</td>\n",
       "      <td>341400000000011622</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2009</td>\n",
       "      <td>2</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24861</th>\n",
       "      <td>340207</td>\n",
       "      <td>6790.0</td>\n",
       "      <td>4500</td>\n",
       "      <td>6</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>340200000000115797</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>110.0</td>\n",
       "      <td>4533</td>\n",
       "      <td>2015</td>\n",
       "      <td>12</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24862</th>\n",
       "      <td>340207</td>\n",
       "      <td>8111.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340207030010000000</td>\n",
       "      <td>340200000000115275</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2012</td>\n",
       "      <td>5</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24863</th>\n",
       "      <td>340225</td>\n",
       "      <td>8090.0</td>\n",
       "      <td>1100</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000116750</td>\n",
       "      <td>341400000000015220</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>260.0</td>\n",
       "      <td>1130</td>\n",
       "      <td>2012</td>\n",
       "      <td>10</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24864</th>\n",
       "      <td>340222</td>\n",
       "      <td>7830.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>6</td>\n",
       "      <td>340222070010000000</td>\n",
       "      <td>340200000020003395</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>2011</td>\n",
       "      <td>5</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10000 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       oplocdistrict  industryco  enttype  state               orgid  \\\n",
       "1             340222      8090.0     9600      6  340222060010000000   \n",
       "5             340207      9019.0     9600      6  340207040010000000   \n",
       "7             340222      8111.0     9600      6  340222060010000000   \n",
       "12            340203      7590.0     1100      6  340203010010000000   \n",
       "13            340202      9051.0     1100      6  340200000000000000   \n",
       "...              ...         ...      ...    ...                 ...   \n",
       "24860         340225      8131.0     9600      6  340200000000116780   \n",
       "24861         340207      6790.0     4500      6  340200000000000000   \n",
       "24862         340207      8111.0     9600      6  340207030010000000   \n",
       "24863         340225      8090.0     1100      7  340200000000116750   \n",
       "24864         340222      7830.0     9600      6  340222070010000000   \n",
       "\n",
       "                    jobid  adbusign  townsign  regtype  regcap  enttypegb  \\\n",
       "1      340200000000112114         0         1        1    10.0       9600   \n",
       "5      400000000000325767         0         0        1    10.0       9600   \n",
       "7      340200000000112114         0         1        1    15.0       9600   \n",
       "12     340200000000100093         0         1        1   500.0       1130   \n",
       "13     400000000000386531         0         1        1    40.0       1130   \n",
       "...                   ...       ...       ...      ...     ...        ...   \n",
       "24860  341400000000011622         0         1        1    20.0       9600   \n",
       "24861  340200000000115797         0         1        1   110.0       4533   \n",
       "24862  340200000000115275         0         1        1    10.0       9600   \n",
       "24863  341400000000015220         0         1        1   260.0       1130   \n",
       "24864  340200000020003395         0         1        1    10.0       9600   \n",
       "\n",
       "       year  month  industryphy  \n",
       "1      2017      9           14  \n",
       "5      2019      9           17  \n",
       "7      2017      7           14  \n",
       "12     2016      2           12  \n",
       "13     2018      5           17  \n",
       "...     ...    ...          ...  \n",
       "24860  2009      2           14  \n",
       "24861  2015     12            9  \n",
       "24862  2012      5           14  \n",
       "24863  2012     10           14  \n",
       "24864  2011      5           13  \n",
       "\n",
       "[10000 rows x 14 columns]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#原数据中的industryphy这一列非pandas可处理的数值格式\n",
    "lbl = preprocessing.LabelEncoder()\n",
    "x_train_full['industryphy'] = lbl.fit_transform(x_train_full['industryphy'].astype(str))\n",
    "x_test_full['industryphy'] = lbl.fit_transform(x_test_full['industryphy'].astype(str))\n",
    "x_test_full"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 864 candidates, totalling 4320 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=5)]: Using backend LokyBackend with 5 concurrent workers.\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "Classification metrics can't handle a mix of binary and continuous targets",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31m_RemoteTraceback\u001b[0m                          Traceback (most recent call last)",
      "\u001b[0;31m_RemoteTraceback\u001b[0m: \n\"\"\"\nTraceback (most recent call last):\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/externals/loky/process_executor.py\", line 431, in _process_worker\n    r = call_item()\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/externals/loky/process_executor.py\", line 285, in __call__\n    return self.fn(*self.args, **self.kwargs)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/_parallel_backends.py\", line 595, in __call__\n    return self.func(*args, **kwargs)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/parallel.py\", line 263, in __call__\n    for func, args, kwargs in self.items]\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/parallel.py\", line 263, in <listcomp>\n    for func, args, kwargs in self.items]\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/model_selection/_validation.py\", line 560, in _fit_and_score\n    test_scores = _score(estimator, X_test, y_test, scorer)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/model_selection/_validation.py\", line 607, in _score\n    scores = scorer(estimator, X_test, y_test)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/metrics/_scorer.py\", line 88, in __call__\n    *args, **kwargs)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/metrics/_scorer.py\", line 213, in _score\n    **self._kwargs)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/utils/validation.py\", line 72, in inner_f\n    return f(**kwargs)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/metrics/_classification.py\", line 187, in accuracy_score\n    y_type, y_true, y_pred = _check_targets(y_true, y_pred)\n  File \"/home/zzx/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/metrics/_classification.py\", line 91, in _check_targets\n    \"and {1} targets\".format(type_true, type_pred))\nValueError: Classification metrics can't handle a mix of binary and continuous targets\n\"\"\"",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-50-18264132b4eb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     39\u001b[0m lgb_grid.fit(x_train_full,y_train_full,eval_set=[(x_test_full, y_test_full)], early_stopping_rounds=400,\n\u001b[0;32m---> 40\u001b[0;31m                           eval_metric='mae')#有了gridsearch我们便不需要fit函数（理论上是这样但是还是要在函数里改一下）\n\u001b[0m\u001b[1;32m     41\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     42\u001b[0m \u001b[0;31m#打印网格搜索的结果\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/utils/validation.py\u001b[0m in \u001b[0;36minner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     70\u001b[0m                           FutureWarning)\n\u001b[1;32m     71\u001b[0m         \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0marg\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 72\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     73\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0minner_f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     74\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/model_selection/_search.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, X, y, groups, **fit_params)\u001b[0m\n\u001b[1;32m    734\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mresults\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    735\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 736\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_search\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevaluate_candidates\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    737\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    738\u001b[0m         \u001b[0;31m# For multi-metric evaluation, store the best_index_, best_params_ and\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/model_selection/_search.py\u001b[0m in \u001b[0;36m_run_search\u001b[0;34m(self, evaluate_candidates)\u001b[0m\n\u001b[1;32m   1186\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_run_search\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mevaluate_candidates\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1187\u001b[0m         \u001b[0;34m\"\"\"Search all candidates in param_grid\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1188\u001b[0;31m         \u001b[0mevaluate_candidates\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mParameterGrid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparam_grid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1189\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1190\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/sklearn/model_selection/_search.py\u001b[0m in \u001b[0;36mevaluate_candidates\u001b[0;34m(candidate_params)\u001b[0m\n\u001b[1;32m    713\u001b[0m                                \u001b[0;32mfor\u001b[0m \u001b[0mparameters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtest\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    714\u001b[0m                                in product(candidate_params,\n\u001b[0;32m--> 715\u001b[0;31m                                           cv.split(X, y, groups)))\n\u001b[0m\u001b[1;32m    716\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    717\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/parallel.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, iterable)\u001b[0m\n\u001b[1;32m   1059\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1060\u001b[0m             \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mretrieval_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1061\u001b[0;31m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mretrieve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1062\u001b[0m             \u001b[0;31m# Make sure that we get a last message telling us we are done\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1063\u001b[0m             \u001b[0melapsed_time\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_start_time\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/parallel.py\u001b[0m in \u001b[0;36mretrieve\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    938\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    939\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'supports_timeout'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 940\u001b[0;31m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_output\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjob\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    941\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    942\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_output\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjob\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/site-packages/joblib/_parallel_backends.py\u001b[0m in \u001b[0;36mwrap_future_result\u001b[0;34m(future, timeout)\u001b[0m\n\u001b[1;32m    540\u001b[0m         AsyncResults.get from multiprocessing.\"\"\"\n\u001b[1;32m    541\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 542\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    543\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mCfTimeoutError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    544\u001b[0m             \u001b[0;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/concurrent/futures/_base.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    430\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mCancelledError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    431\u001b[0m             \u001b[0;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_state\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mFINISHED\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 432\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__get_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    433\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    434\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/py3.6/lib/python3.6/concurrent/futures/_base.py\u001b[0m in \u001b[0;36m__get_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    382\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__get_result\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    383\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_exception\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 384\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_exception\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    385\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    386\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Classification metrics can't handle a mix of binary and continuous targets"
     ]
    }
   ],
   "source": [
    "# parameters = {'nthread':[2,4,5,6,7,8], #when use hyperthread, xgboost may become slower\n",
    "#               'num_leaves':[32,64,70,80,90], #不要超过最大层数的幂方，太接近过造成过拟合\n",
    "#               'learning_rate': [.01, .03, 0.05], #尽可能小\n",
    "#               'num_iterations':[300,500,700,900],#尽可能大\n",
    "#               'boosting':['dart','goss','gbdt'], #尝试dart\n",
    "#               'max_depth': [ 7, 8, 9], #2^7=128\n",
    "#               'min_child_weight': [1,2,3,4,5,6],\n",
    "#               'silent': [1],\n",
    "#               'subsample': [0.1,0.3,0.5,0.7,0.8],\n",
    "#               'colsample_bytree': [.5,.6,.7,.8],#the subsample ratio of columns when constructing each tree,特征抽样\n",
    "#               'n_estimators': [400,500,600,700]\n",
    "#              }\n",
    "\n",
    "# parameters = {'nthread':[2], #when use hyperthread, xgboost may become slower\n",
    "#               'num_leaves':[32,64], #不要超过最大层数的幂方，太接近过造成过拟合\n",
    "#               'learning_rate': [.01], #尽可能小\n",
    "#               'num_iterations':[900],#尽可能大\n",
    "#               'boosting':['dart','goss','gbdt'], #尝试dart\n",
    "#               'max_depth': [ 7, 8, 9], #2^7=128\n",
    "#               'min_child_weight': [1,2,3,4],\n",
    "#               'silent': [1],\n",
    "#               'subsample': [0.1,0.3,0.5],\n",
    "#               'colsample_bytree': [.5,.6],#the subsample ratio of columns when constructing each tree,特征抽样\n",
    "#               'n_estimators': [400,500]\n",
    "#              }\n",
    "\n",
    "lgb_model = lgb.LGBMRegressor(\n",
    "    num_leaves=64, reg_alpha=0., reg_lambda=0.01, metric='mae',\n",
    "    max_depth=-1, learning_rate=0.05, min_child_samples=10, seed=2020,\n",
    "    n_estimators=2000, subsample=0.7, colsample_bytree=0.7, subsample_freq=1)\n",
    "\n",
    "# lgb_grid = GridSearchCV(lgb_model,\n",
    "#                         parameters,#网格搜索最佳参数\n",
    "#                         cv = 5,#交叉验证数\n",
    "#                         n_jobs = 5,\n",
    "#                         scoring='accuracy',#以准确率为搜索标准\n",
    "#                         verbose=True)\n",
    "\n",
    "lgb_grid.fit(x_train_full,y_train_full,eval_set=[(x_test_full, y_test_full)], early_stopping_rounds=400,\n",
    "                          eval_metric='mae')#有了gridsearch我们便不需要fit函数（理论上是这样但是还是要在函数里改一下）\n",
    "\n",
    "#打印网格搜索的结果\n",
    "print(lgb_grid.best_score_)\n",
    "print(lgb_grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'lgb_model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-fe8295a6ba9f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     13\u001b[0m               }\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m lgb_grid = GridSearchCV(lgb_model,\n\u001b[0m\u001b[1;32m     16\u001b[0m                         \u001b[0mparameters\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m                         \u001b[0mscoring\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'accuracy'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;31m#这个是一开始没加进去的\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'lgb_model' is not defined"
     ]
    }
   ],
   "source": [
    "parameters = {'nthread':[2,4,5,6,7,8], #when use hyperthread, xgboost may become slower\n",
    "              'objective':['reg:linear','reg:squarederror','reg:squaredlogerror','reg:logistic','reg:pseudohubererror'],\n",
    "              'learning_rate': [.01,.03, 0.05], #so called `eta` value\n",
    "              'max_depth': [7, 8, 9],\n",
    "              'min_child_weight': [1,2,3,4,5,6]  ,\n",
    "              'silent': [1],\n",
    "              'subsample': [0.3,0.5,0.7,0.8],\n",
    "              'colsample_bytree': [.5,.6,.7,.8],#the subsample ratio of columns when constructing each tree\n",
    "              'n_estimators': [400,500,600,700],\n",
    "              'device':'gpu',\n",
    "              'gpu_platform_id':0,\n",
    "              'gpu_device_id':0              \n",
    "              }\n",
    "\n",
    "lgb_grid = GridSearchCV(lgb_model,\n",
    "                        parameters,\n",
    "                        scoring='accuracy',#这个是一开始没加进去的\n",
    "                        cv = 5,#交叉验证数\n",
    "                        n_jobs = 5,\n",
    "                        verbose=True)\n",
    "\n",
    "lgb_grid.fit(x_train_full,y_train_full)\n",
    "\n",
    "print(lgb_grid.best_score_)\n",
    "print(lgb_grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
